package com.zhangxiaofanfan.hotcode.third;

import java.util.HashMap;
import java.util.Map;

/**
 * TODO
 *
 * @author zhangxiaofanfan
 * @date 2024-05-30 08:25:58
 */
public class HotCode040 {
    public static void main(String[] args) {

    }

    private int maxLength = 0;
    public int diameterOfBinaryTree(TreeNode root) {
        bfs(root);
        return Math.max(0, maxLength - 1);
    }

    public int bfs(TreeNode node) {
        if (node == null) {
            return 0;
        }
        int left = bfs(node.left);
        int right = bfs(node.right);
        maxLength = Math.max(maxLength, left + 1 + right);
        return Math.max(left, right) + 1;
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
